/**
 * Created by Administrator on 2017/4/28.
 */
$(function(){
    var userData = localStorage.getItem('youyuUserData');
    var objUser = $.parseJSON(userData);
    var wxOpenid = objUser.wxopenid;
    packAge();

    var dataN = 0;
    var isClick = false;
      $('#list').delegate('.checks','click',function(){//事件委托，
          isClick = true;
        $(".checked").removeClass("checked").addClass("checks");
        dataN = $(this).attr('data-value');
        $(this).removeClass("checks").addClass("checked");

    });
    var payClick =0;
$(".payBtn").bind('click',function(){
    //调取微信支付接口；
    if(isClick==true){
       if(payClick == 0){
           payClick = 1;
           payWX();
           setTimeout(function () { payClick =0; }, 2000);
       }

    }
})


    //获取套餐ID;
    function packAge(){
          var packUrl = QUERY_CHANEL;
          var curTime = new Date().getTime();
          var packParam ={'channel':'2'};
        var pack = {
        time: curTime,
        url:packUrl,
        param:packParam,
        sign:$.md5('ts='+curTime+'&mn=3&chid=0&version=1.0&param='+JSON.stringify(packParam)),
        okCallback:function(data){
            var dataPay = document.getElementById('payWx').innerHTML;
             var html = template(dataPay , data);
             $("#list").append(html);
        },
        errorCallback:function(){
        }
    }
    YY.post(pack);
    }
    /*支付*/
   function payWX(){
       var token = localStorage.getItem('youyuLoginToken');
       var youyuOpenid =  localStorage.getItem('youyuOpenid');
     var openID = youyuOpenid;
     var packageNumber=dataN;
     var payUrl =PAYWEIXIN;
     var param = {'packageNumber':packageNumber,'chId':'WechatServiceAccountPay','openid':openID};
     var dateTime = new Date().getTime();
     var wxPay = {
     time:dateTime,
     token:token,
     url:payUrl,
     param:param,
     sign:$.md5('ts='+ dateTime +'&mn=3&chid=0&version=1.0&token='+token+'&param='+JSON.stringify(param)),
     okCallback:function(data){
         console.log(data);
        var appId =data.data.appId;
         var timestamp = data.data.timeStamp;
         var noncestr = data.data.nonceStr;
         var packAge = data.data.packageValue;
         var sign = data.data.paySign;
         var signType = data.data.signType;
      function onBridgeReady(){
             WeixinJSBridge.invoke(
                 'getBrandWCPayRequest', {
                     "appId":appId,     //公众号名称，由商户传入
                     "timeStamp":timestamp,         //时间戳，自1970年以来的秒数
                     "nonceStr":noncestr, //随机串
                     "package":packAge,
                     "signType":signType,         //微信签名方式：
                     "paySign":sign //微信签名
                 },
                 function(res){
                     if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                         userGold();
                     }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。
                 }
             );
         }
        if (typeof WeixinJSBridge == "undefined"){
             if( document.addEventListener ){
                 document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
             }else if (document.attachEvent){
                 document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                 document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
             }
         }else{
             onBridgeReady();
         }

     },
     errorCallback:function(){
     }
     }
     YY.post(wxPay);
     }
    function userGold(){
        var token = localStorage.getItem('youyuLoginToken');
        var packUrl = USER_ACCOUNT;
        var curTime = new Date().getTime();
        var uid = $.parseJSON(userData).uid;
        var packParam ={'uid':uid};
        var gold = {
            time: curTime,
            token:token,
            url:packUrl,
            param:packParam,
            sign:$.md5('ts='+curTime+'&mn=3&chid=0&version=1.0&token='+token+'&param='+JSON.stringify(packParam)),
            okCallback:function(data){
                golds = data.data+'金币';
                $('#gold').text(golds);
            },
            errorCallback:function(){
            }
        }
        YY.post(gold);
    }
})

